package LandR.login.loginSrc.servlet;


import LandR.login.loginSrc.function.Help.JsonHelp;
import LandR.login.loginSrc.function.Help.MysqlFunctionHelpUtil;
import LandR.login.loginSrc.function.User.User;
import token.Token;
import token.TokenHelp;
import token.TokenJson;
import token.TokenThread;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.sql.SQLException;

public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");

        InputStreamReader insr = new InputStreamReader(request.getInputStream(), StandardCharsets.UTF_8);
        StringBuilder result = new StringBuilder();
        int respInt = insr.read();
        while (respInt != -1) { // 读取请求数据
            result.append((char) respInt);
            respInt = insr.read();
        }
        System.out.println("前端传递的登录数据为:" + result);
        insr.close();
        String json = result.toString();
        JsonHelp change = new JsonHelp();
        User user = change.jsonToJavaObjectByFastJson(json);

        System.out.println("开始登录");
        MysqlFunctionHelpUtil mfhu = new MysqlFunctionHelpUtil();
        int can = mfhu.judgeMethodForRegister(user.getName());
        /**
         * can内储存着登录方式的判定结果 1/用户名 2/手机号 3/邮箱
         */

        PrintWriter out = response.getWriter();

        switch (can) {
            case 1:
                System.out.println("---用户名登录开始---");
                user.setUsername(user.getName());

                System.out.println("对象信息为：" + user.toString());
                user = mfhu.selectUserFromDataByUsername(user);
                if (user.getId() != -1) {

                    TokenHelp th = new TokenHelp();
                    String str = th.gainToken();
                    Token token = new Token();
                    token.setToken(str);
                    TokenJson tj = new TokenJson();
                    str = tj.javaToJsonObjectByFastJson(token);

                    out.print(str);

                    System.out.println("登录的token为：" + token);

                    TokenThread tt = new TokenThread("用户" + user.getId().toString());
                    tt.setToken(str);
                    tt.setUser_id(user.getId());
                    tt.start();

                    System.out.println("用户登录token已返回前端!!!");
                    System.out.println("---登录成功！---");

                    try {
                        //更新users数据表中的token
                        th.updateTokenForUsers(user.getId(), token.getToken());
                        //更新usersdata数据表中的token
                        th.updateTokenForUsersData(user.getId(), token.getToken());
                        //更新users数据表中的thread_ID
                        th.updateThreadIDForUsers(user.getId(), tt.getId());
                        //更新allnotes数据表中的token
                        th.updateTokenForAllNotes(user.getId(), token.getToken());


                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }


                } else {
                    out.print("登录失败！用户名或密码错误");
                }

                break;
            case 2:
                System.out.println("---手机号登录开始---");
                user.setPhoneNumber(user.getName());
                user = mfhu.selectUserFromDataByePhoneNumber(user);
                if (user.getId() != -1) {
                    TokenHelp th = new TokenHelp();
                    String str = th.gainToken();
                    Token token = new Token();
                    token.setToken(str);
                    TokenJson tj = new TokenJson();
                    str = tj.javaToJsonObjectByFastJson(token);
                    out.print(str);

                    System.out.println("登录的token为：" + token);
                    TokenThread tt = new TokenThread(user.getId().toString());
                    tt.setUser_id(user.getId());
                    tt.setToken(str);
                    tt.start();

                    System.out.println("用户登录token已返回前端!!!");
                    System.out.println("---登录成功！---");

                    try {
                        //更新users数据表中的token
                        th.updateTokenForUsers(user.getId(), token.getToken());
                        //更新usersdata数据表中的token
                        th.updateTokenForUsersData(user.getId(), token.getToken());
                        //更新users数据表中的thread_ID
                        th.updateThreadIDForUsers(user.getId(), tt.getId());
                        //更新allnotes数据表中的token
                        th.updateTokenForAllNotes(user.getId(), token.getToken());

                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                } else {
                    out.print("登录失败！用户名或密码错误");
                }
                break;
            case 3:
                System.out.println("---邮箱登录开始---");
                user.setEmail(user.getName());
                user = mfhu.selectUserFromDataByemail(user);
                if (user.getId() != -1) {
                    TokenHelp th = new TokenHelp();
                    String str = th.gainToken();
                    Token token = new Token();
                    token.setToken(str);
                    TokenJson tj = new TokenJson();
                    str = tj.javaToJsonObjectByFastJson(token);

                    out.print(str);

                    System.out.println("登录的token为：" + token);

                    TokenThread tt = new TokenThread(user.getId().toString());
                    tt.setUser_id(user.getId());
                    tt.setToken(str);
                    tt.start();

                    System.out.println("用户登录token已返回前端!!!");
                    System.out.println("---登录成功！---");

                    try {
                        //更新users数据表中的token
                        th.updateTokenForUsers(user.getId(), token.getToken());
                        //更新usersdata数据表中的token
                        th.updateTokenForUsersData(user.getId(), token.getToken());
                        //更新users数据表中的thread_ID
                        th.updateThreadIDForUsers(user.getId(), tt.getId());
                        //更新allnotes数据表中的token
                        th.updateTokenForAllNotes(user.getId(), token.getToken());
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                } else {
                    out.print("登录失败！用户名或密码错误");
                }
                break;
            default:
                out.print("登录失败！");
        }
        System.out.println("---登录功能结束---");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
